import Cookie from '@/libs/js.cookie.min'
export default class Global{
  static setTitle(title){
    uni.setNavigationBarTitle({
      title
    })
  }
  static isWxmini(){
    return new Promise(resolve => {
      function ready() {
        resolve(window.__wxjs_environment === 'miniprogram');
      }
      if (!window.WeixinJSBridge || !WeixinJSBridge.invoke) {
        document.addEventListener('WeixinJSBridgeReady', ready, false)
      } else {
        ready()
      }
    })
  }
  static checkAuth(){
    if(process.env.NODE_ENV == 'development'){
      return Promise.resolve();
    }else{
      return new Promise(resolve => {
        //判断是否在微信中，在微信，不用管，不再微信跳转微信登录
        this.checkLogin().then(res => {
          resolve();
        },err => {
          this.jumpWxAuth();
          //uni.showToast({
            //title : `发生错误，请重试`,
            //icon : 'none'
          //})
        })
      })
    }
  }
  static checkLogin(){
    let has = Cookie.get('PHPSESSID');
    if(has){
      return Promise.resolve();
    }else{
      return Promise.reject();
    }
  }
  static jumpWxAuth(){
    let { sid } = uni.getStorageSync(CJKEY);
    location.replace('/wap/subject/index?sid=' + sid);
  }
  static isInWx(){
    var ua = navigator.userAgent.toLowerCase();
    var isWeixin = ua.indexOf('micromessenger') != -1;
    return isWeixin;
  }
  static is_iPhoneX(){
    if (typeof window !== 'undefined' && window) {
      return /iphone/gi.test(window.navigator.userAgent) && window.screen.height >= 724;
    }
    return false;
  }
}
